package stdying.handyweibo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import stdying.handyweibo.injection.component.DaggerMainActivityComponent;
import stdying.handyweibo.injection.component.MainActivityComponent;
import stdying.handyweibo.injection.module.MainActivityModule;
import stdying.handyweibo.openapi.TokenSession;
import stdying.handyweibo.presenter.MainActivityPresenter;
import stdying.handyweibo.ui.BaseActivity;
import stdying.handyweibo.ui.auth.WBAuthActivity;
import stdying.handyweibo.view.IMainView;
import timber.log.Timber;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener ,IMainView {

    private MainActivityComponent mainActivityComponent;

    @Inject
    MainActivityPresenter mainActivityPresenter;

    @Bind(R.id.tb_main)
    Toolbar mToolbar;

    @Bind(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;

    @Bind(R.id.nan_view)
    NavigationView mNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.drawer_main);
        ButterKnife.bind(this);

        //使用toolbar代替actionBar
        setSupportActionBar(mToolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawerLayout.setDrawerListener(toggle);
        toggle.syncState();

        //设置每个空间事件监听
        mNavigationView.setNavigationItemSelectedListener(this);
        Log.d("test","test");
        Timber.i("查询用户信息");
        if(TokenSession.getTokenType(this) == -1){
            Intent intentAuth = new Intent();
            intentAuth.setClass(this, WBAuthActivity.class);
            startActivity(intentAuth);
        }else{
            Timber.i("查询用户信息");
            mainActivityPresenter.getUserInfo();
        }

        mainActivityPresenter.getLastMyFriends();

    }


    @Override
    public void onBackPressed() {
        //关闭抽屉
        if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }else{
            super.onBackPressed();
        }

    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        int id = menuItem.getItemId();

        switch (id) {
            case R.id.nav_camara:
                break;
            case R.id.nav_gallery:
                break;
            case R.id.nav_share:
                break;
        }

        //关闭抽屉
        mDrawerLayout.closeDrawer(GravityCompat.START);

        return true;
    }

    /**
     * 设置侧滑菜单头像
     */
    public void refreshUserInfo(){

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    //初始化component
    @Override
    protected void initInjector() {
        mainActivityComponent = DaggerMainActivityComponent.builder()
                .mainActivityModule(new MainActivityModule(this))
                .build();
        mainActivityComponent.inject(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showError(String msg) {

    }

}
